package com.goodving.guli.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.ui.DimPanelBottomBar;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;

/**
 * Created by john on 2015/12/9.
 */
public class MemberInfoSetNameActivity extends MemberInfoBaseActivity implements View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {
    private View mView;
    private DimPanelBottomBar mDimPanelBottomBar;
    private EditText mEditText;
    private InputMethodManager mInputMethodManager;
    private Context mContext;
    private TextPaint mTextPaint;
    private LayoutParams mLayoutParams;
    private String name;
    private boolean isFromBabyWeight;

    public MemberInfoSetNameActivity() {
        this.mContext = this;
        this.name = C3657a.f18328f;
        this.isFromBabyWeight = false;
    }

    public void onCancel() { // m15830c //m15809c
        super.onCancel();
    }

    public void onConfirm() { // m15810d
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "set username " + this.name);
        if (!this.isVisitorMode) {
            super.onConfirm();
        }
        if (!this.isVisitorMode) {
            if (this.name == null || this.name.trim().isEmpty()) {
                C3247b.m22930a((Context) this, getResources().getString(R.string.please_input_name), 0).show();
                return;
            } else if (!Utils.m8072g(this.name)) {
                C3247b.m22930a((Context) this, getResources().getString(R.string.invalid_name), 0).show();
                return;
            }
        }
        Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, this.name.trim());
        Intent intent = new Intent();
        intent.setClass(this, MemberInfoSetGenderActivity.class);
        intent.putExtra(MemberInfoBaseActivity.setVisitorMode, this.isVisitorMode);
        intent.putExtra(MemberInfoBaseActivity.weightChooseUser, this.isWeightChooseUser);
        intent.putExtra("FROM_BABY_WEIGHT", this.isFromBabyWeight);
        intent.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11393g == null ? C3657a.f18328f : this.f11393g.m25503n());
        startActivityForResult(intent, 6);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "isWeightChooseUser " + this.isWeightChooseUser + " isVisitorMode " + this.isVisitorMode);
        if (!(this.isWeightChooseUser || this.isVisitorMode || i2 != -1)) {
            C3247b.m22930a(this.mContext, getString(R.string.generate_user_succ), 0).show();
        }
        if (!this.isWeightChooseUser || !this.isVisitorMode) {
            super.onActivityResult(i, i2, intent);
        } else if (i2 == 0) {
            setResult(0);
            finish();
        } else {
            setResult(-1);
            finish();
        }
    }

    public void onBackPressed() {
        super.onBackPressed();
        Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, C3657a.f18328f);
    }

    class NameTextWatcher implements TextWatcher {
        final /* synthetic */ MemberInfoSetNameActivity setNameActivity;

        NameTextWatcher(MemberInfoSetNameActivity memberInfoSetNameActivity) {
            this.setNameActivity = memberInfoSetNameActivity;
        }

        public void afterTextChanged(Editable editable) {
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "afterTextChanged");
            this.setNameActivity.name = this.setNameActivity.mEditText.getText().toString();
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            GuliLog.tracePrintInfo("MemberInfoBaseActivity", "onTextChanged " + charSequence.toString());
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.isVisitorMode = getIntent().getBooleanExtra(MemberInfoBaseActivity.setVisitorMode, false);
        this.isWeightChooseUser = getIntent().getBooleanExtra(MemberInfoBaseActivity.weightChooseUser, false);
        this.isFromBabyWeight = getIntent().getBooleanExtra("FROM_BABY_WEIGHT", false);
        if (!(getIntent() == null || getIntent().getExtras() == null)) {
            this.f11393g = ScaleWeightInfo.m25479b(getIntent().getStringExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY));
        }
        if (this.isWeightChooseUser && this.isVisitorMode) {
            Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, C3657a.f18328f);
            onConfirm();
            return;
        }
        setContentView((int) R.layout.activity_person_info_set_name);
        this.mInputMethodManager = (InputMethodManager) this.mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        m15808b();
        this.mEditText = (EditText) findViewById(R.id.info_name_edittext);
        this.name = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME);
        if (this.name != null) {
            this.mEditText.setText(this.name);
        }
        this.mTextPaint = this.mEditText.getPaint();
        this.mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
        this.mEditText.addTextChangedListener(new NameTextWatcher(this));
        this.mDimPanelBottomBar = (DimPanelBottomBar) findViewById(R.id.bottom_bar);

        this.mDimPanelBottomBar.m14980a(getResources().getColor(R.color.content_color));
        this.mDimPanelBottomBar.m14983b(getResources().getColor(R.color.title_color));

        this.mLayoutParams = new LayoutParams(-1, -2);
        this.mLayoutParams.addRule(12);
        this.mView = findViewById(R.id.info_gender_bg);
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void onGlobalLayout() {
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int i = rect.bottom - rect.top;
        int height = getWindow().getDecorView().getHeight();
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "rawHeight is " + height + " displayHeight is " + i);
        GuliLog.tracePrintInfo("MemberInfoBaseActivity", "is softInputActivt " + this.mInputMethodManager.isActive());
        if (height - i > 300) {
            this.mLayoutParams.bottomMargin = 0;
            this.mDimPanelBottomBar.setLayoutParams(this.mLayoutParams);
            return;
        }
        this.mLayoutParams.bottomMargin = Utils.m7963a(this.mContext, 36.0f);
        this.mDimPanelBottomBar.setLayoutParams(this.mLayoutParams);
    }

    protected void onPause() {
        super.onPause();
        if (Build.VERSION.SDK_INT >= 16) {
            getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        C0668a.m8141b(C0668a.aM);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aM);
        C0668a.m8124a((Activity) this);
    }
}